﻿<template>
  <div class="app-container">
    <div class="filter-title">
      <el-form label-position="right" label-width="95px">
        <el-form-item :label="$t('order.no')+' :'">
          <el-input
            v-model="listQuery.order_no"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('order.no')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('charge.type')+' :'">
          <el-select
            v-model="listQuery.type"
            clearable
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('charge.type')"
          >
            <el-option
              v-for="item in typeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('order.username')+' :'">
          <el-input
            v-model="listQuery.username"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('order.username')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-button
          style="margin-left:20px;float:right"
          class="filter-item"
          type="primary"
          @click="handleRecharge"
        >{{ $t('table.Recharge') }}</el-button>
        <el-button
          style="margin-left:20px"
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >{{ $t('table.search') }}</el-button>
        <el-button
          style="margin-left: 10px"
          class="filter-item"
          type="primary"
          @click="handleRefresh"
        >{{ $t('table.refresh') }}</el-button>
        <div v-show="!showSearchItem" class="showDown" @click="showSearchItem = true">
          <i class="el-icon-d-arrow-right"></i>
        </div>
        <div v-show="showSearchItem" class="showUp" @click="showSearchItem = false">
          <i class="el-icon-d-arrow-left"></i>
        </div>
      </el-form>
    </div>
    <div v-show="showSearchItem" class="filter-container">
      <el-form label-position="right" label-width="95px">
        <el-button
          style="margin-left:20px"
          class="filter-item"
          type="primary"
          @click="timeFilter(1)"
        >今日</el-button>
        <el-button
          style="margin-left:20px"
          class="filter-item"
          type="primary"
          @click="timeFilter(2)"
        >昨日</el-button>
        <el-button
          style="margin-left:20px"
          class="filter-item"
          type="primary"
          @click="timeFilter(3)"
        >近一周</el-button>
        <el-button
          style="margin-left:20px"
          class="filter-item"
          type="primary"
          @click="timeFilter(4)"
        >近一月</el-button>
        <el-form-item :label="$t('order.createTime')+' :'">
          <div class="filter-item">
            <el-date-picker
              v-model="listQuery.startTime"
              :picker-options="pickerOptionsB"
              type="date"
              :placeholder="$t('public.startTime')"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </div>
          <div class="filter-item">
            <el-date-picker
              v-model="listQuery.endTime"
              :picker-options="pickerOptionsE"
              type="date"
              :placeholder="$t('order.endTime')"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(255,255,255,0.2)"
      border
      fit
      highlight-current-row
      style="width: 100%min-height:1000px"
    >
      <el-table-column fixed="left" align="center" :label="$t('table.id')" width="65">
        <template slot-scope="scope">
          <span>{{ (listQuery.page-1)*listQuery.limit+scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('order.no')">
        <template slot-scope="scope">
          <span>{{ scope.row.order_no }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('order.username')">
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('charge.type')">
        <template slot-scope="scope">
          <span>{{ paymentMethodEs(scope.row.type) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('charge.money')">
        <template slot-scope="scope">
          <span>{{ scope.row.money }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('order.createTime')">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('public.status')">
        <template slot-scope="scope">
          <span
            :class="statusColorSecond(scope.row.check_status)"
          >{{ statusEstimation(scope.row.check_status) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        align="center"
        :label="$t('table.actions')"
        :width="operWidth"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleRead(scope.row)"
          >{{ $t('table.read') }}</el-button>
          <el-button
            v-if="permission.review"
            type="primary"
            size="mini"
            :disabled="(scope.row.check_status === 1 || scope.row.check_status === 3)?false:true"
            @click="handleReview(scope.row)"
          >{{ $t('table.review') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
    <!-- 编辑 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="right"
        label-width="35%"
        style="width: 400px margin-left:120px"
        size="small"
      >
        <el-form-item :label="$t('order.no')+' :'">
          <span>{{ temp.order_no }}</span>
        </el-form-item>
        <el-form-item :label="$t('charge.type')+' :'">
          <span>{{ paymentMethodEs(temp.type) }}</span>
        </el-form-item>
        <el-form-item :label="$t('charge.money')+' :'">
          <span>{{ temp.money }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.name')+' :'">
          <span>{{ temp.name }}</span>
        </el-form-item>
        <el-form-item v-if="temp.account" :label="$t('order.account')+' :'">
          <span>{{ temp.account }}</span>
        </el-form-item>
        <el-form-item v-if="temp.bank_name" :label="$t('order.bankName')+' :'">
          <span>{{ temp.bank_name }}</span>
        </el-form-item>
        <el-form-item v-if="temp.picture" :label="$t('order.picture')+' :'">
          <img :src="temp.picture" width="150px" height="auto" />
        </el-form-item>
        <el-form-item v-if="temp.remark" :label="$t('public.remark')+' :'">
          <span>{{ temp.remark }}</span>
        </el-form-item>
        <el-form-item v-if="temp.type===4" :label="$t('order.dealTime')+' :'">
          <span>{{ timeToData(temp.deal_time) }}</span>
        </el-form-item>
        <el-form-item :label="$t('order.buyerId')+' :'">
          <span>{{ temp.buyer_id }}</span>
        </el-form-item>
        <el-form-item :label="$t('order.buyerType')+' :'">
          <span>{{ buyerTypeEs(temp.buyer_type) }}</span>
        </el-form-item>
        <el-form-item :label="$t('order.username')+' :'">
          <span>{{ temp.username }}</span>
        </el-form-item>
        <el-form-item :label="$t('order.adCurrency')+' :'">
          <span>{{ temp.ad_currency }}</span>
        </el-form-item>
        <el-form-item :label="$t('order.extraCurrency')+' :'">
          <span>{{ temp.extra_currency }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.region')+' :'">
          <span>{{ temp.region_name }}</span>
        </el-form-item>
        <el-form-item :label="$t('order.status')+' :'">
          <span>{{ statusEstimation(temp.check_status) }}</span>
        </el-form-item>
        <el-form-item :label="$t('order.createTime')+' :'">
          <span>{{ temp.create_time }}</span>
        </el-form-item>
        <el-form-item
          v-if="dialogStatus=='review'"
          :label="$t('order.checkStatus')+' :'"
          prop="check_status"
        >
          <el-radio-group v-model="temp.check_status">
            <el-radio :label="1" disabled>审核中</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">未通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-if="temp.check_status === 3"
          :label="$t('public.auditOpinion')+' :'"
          prop="check_remark"
        >
          <el-input v-model="temp.check_remark" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button
          type="primary"
          :disabled="!createDisabled"
          @click="reviewData"
        >{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>

    <!-- 查看 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormRead">
      <el-form
        :model="temp"
        label-position="right"
        label-width="40%"
        style="width: 400px margin-left:120px"
      >
        <el-form-item :label="$t('order.no')+' :'">
          <span>{{ temp.order_no }}</span>
        </el-form-item>
        <el-form-item :label="$t('charge.type')+' :'">
          <span>{{ paymentMethodEs(temp.type) }}</span>
        </el-form-item>
        <el-form-item :label="$t('charge.money')+' :'">
          <span>{{ temp.money }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.name')+' :'">
          <span>{{ temp.name }}</span>
        </el-form-item>
        <el-form-item v-if="temp.account" :label="$t('order.account')+' :'">
          <span>{{ temp.account }}</span>
        </el-form-item>
        <el-form-item v-if="temp.bank_name" :label="$t('order.bankName')+' :'">
          <span>{{ temp.bank_name }}</span>
        </el-form-item>
        <el-form-item v-if="temp.picture" :label="$t('order.picture')+' :'">
          <img :src="temp.picture" width="150px" height="auto" />
        </el-form-item>
        <el-form-item v-if="temp.remark" :label="$t('public.remark')+' :'">
          <span>{{ temp.remark }}</span>
        </el-form-item>
        <el-form-item v-if="temp.type===4" :label="$t('order.dealTime')+' :'">
          <span>{{ timeToData(temp.deal_time) }}</span>
        </el-form-item>
        <el-form-item :label="$t('order.buyerId')+' :'">
          <span>{{ temp.buyer_id }}</span>
        </el-form-item>
        <el-form-item :label="$t('order.buyerType')+' :'">
          <span>{{ buyerTypeEs(temp.buyer_type) }}</span>
        </el-form-item>
        <el-form-item :label="$t('order.username')+' :'">
          <span>{{ temp.username }}</span>
        </el-form-item>
        <el-form-item :label="$t('order.adCurrency')+' :'">
          <span>{{ temp.ad_currency }}</span>
        </el-form-item>
        <el-form-item :label="$t('order.extraCurrency')+' :'">
          <span>{{ temp.extra_currency }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.region')+' :'">
          <span>{{ temp.region_name }}</span>
        </el-form-item>
        <el-form-item :label="$t('order.status')+' :'">
          <span>{{ statusEstimation(temp.check_status) }}</span>
        </el-form-item>
        <el-form-item :label="$t('order.createTime')+' :'">
          <span>{{ temp.create_time }}</span>
        </el-form-item>
        <el-form-item v-if="temp.check_status === 3" :label="$t('public.auditOpinion')+' :'">
          <span>{{ temp.check_remark }}</span>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getToken } from "@/utils/auth"; // get token from cookie

export default {
  name: "Order",
  data() {
    const remark = (rule, value, callback) => {
      if (value) {
        if (value.length <= 0) {
          callback(new Error("请输入审核意见"));
        } else if (value.length > 300) {
          callback(new Error("审核意见不能超过300个字符"));
        } else {
          callback();
        }
      } else {
        callback(new Error("请输入审核意见"));
      }
    };
    return {
      showSearchItem: false,
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      type: "",
      typeOptions: [
        {
          value: 1,
          label: "微信"
        },
        {
          value: 2,
          label: "支付宝"
        },
        {
          value: 3,
          label: "转账"
        },
        {
          value: 4,
          label: "现金"
        },
        {
          value: 5,
          label: "易物"
        }
      ],
      listQuery: {
        page: 1,
        limit: 20,
        name: "",
        status: "",
        play_status: "",
        regions_name: "",
        trade_name: "",
        startTime: "",
        endTime: ""
      },
      temp: {
        id: undefined,
        check_status: "",
        check_remark: ""
      },
      temptime: [{ start: "", end: "" }],
      dialogFormVisible: false,
      dialogFormRead: false,
      dialogStatus: "",
      upLoading: false,
      visibleVideo: false,
      textMap: {
        update: "更新",
        create: "添加",
        review: "审核",
        read: "查看"
      },
      rules: {
        check_status: [
          { required: true, message: "请审核", trigger: "change" }
        ],
        check_remark: [{ required: true, trigger: "blur", validator: remark }]
      },
      createDisabled: true,
      updateDisabled: true,
      loading: false,
      industryOptions: [],
      areaOptions: [],
      areaCount: undefined,
      videoOptions: [],
      videoCount: undefined,
      industryCount: undefined,
      pageSize: 20,
      equipmentOptions: [],
      equipmentCount: undefined,
      permission: {
        review: false
      },
      pickerOptionsB: {
        // 限制时间
        // selectableRange: ['22:00:00 - 23:59:59','00:00:00 - 02:00:00'],
        // 限制日期
        disabledDate: this.disabledDateB
      },
      pickerOptionsE: {
        disabledDate: this.disabledDateE
      },
      pickerOptionsBT: {
        disabledDate: this.disabledDateBT
      },
      pickerOptionsET: {
        disabledDate: this.disabledDateET
      }
    };
  },
  computed: {
    operWidth: function() {
      let itemCount = 1;
      if (this.permission.review) {
        itemCount += 1;
      }
      return itemCount * 80 + "px";
    }
  },
  created() {
    if (this.$store.state.permission.jurisdiction === "*") {
      this.permission.review = true;
    } else {
      const jurisdiction = this.$store.state.permission.jurisdiction.split(",");
      jurisdiction.forEach(element => {
        const id = parseInt(element);
        if (id === 212) {
          this.permission.review = true;
        }
      });
    }
    this.type = JSON.parse(getToken()).type;
    this.getList();
  },
  methods: {
    disabledDateB(time) {
      if (this.listQuery.endTime) {
        return (
          time.getTime() >
          new Date(this.listQuery.endTime).getTime() + 24 * 3600 * 1000 - 8.64e7
        );
      }
    },
    disabledDateE(time) {
      if (this.listQuery.startTime) {
        return (
          time.getTime() < new Date(this.listQuery.startTime).getTime() - 8.64e6
        );
      }
    },
    disabledDateBT(time) {
      if (this.temp.end_time) {
        return (
          time.getTime() >
          new Date(this.temp.end_time).getTime() + 24 * 3600 * 1000 - 8.64e7
        );
      }
    },
    disabledDateET(time) {
      if (this.temp.start_time) {
        return (
          time.getTime() < new Date(this.temp.start_time).getTime() - 8.64e6
        );
      }
    },
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page
      };
      if (this.listQuery.order_no) {
        data["order_no"] = this.listQuery.order_no;
      }
      if (this.listQuery.type) {
        data["type"] = this.listQuery.type;
      }
      if (this.listQuery.status) {
        data["status"] = this.listQuery.status;
      }
      if (this.listQuery.username) {
        data["username"] = this.listQuery.username;
      }
      if (this.listQuery.startTime) {
        data["startTime"] = this.listQuery.startTime;
      }
      if (this.listQuery.endTime) {
        const date = new Date(this.listQuery.endTime);
        data["endTime"] = this.timestampToTime(
          date.getTime() + (60 * 60 * 24 * 1000 - 1)
        );
      }
      if (this.listQuery.time) {
        data["time"] = this.listQuery.time;
      }

      const adPlayRuleListPath = {
        // 获取规则列表
        company: "STQ",
        api: "pc_order_index",
        version: "1.0",
        data: data
      };
      this.socketApi.sendSock(JSON.stringify(adPlayRuleListPath), res => {
        if (res) {
          this.socketData(res);
          this.listLoading = false;
        } else {
          this.$message.error(" 获取视频源列表失败 ");
          this.listLoading = false;
        }
      });
    },
    timeFormat(time) {
      const date = new Date(time);
      const Y = date.getFullYear();
      const M =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1;
      const D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      return Y + "." + M + "." + D;
    },
    timeFilter(data) {
      this.listQuery.time = data;
      this.listQuery.page = 1;
      this.getList();
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleRefresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        name: "",
        status: "",
        play_status: "",
        regions_name: "",
        trade_name: "",
        startTime: "",
        endTime: ""
      };
      this.getList();
    },
    handleReview(row) {
      // 打开审核弹窗
      this.temp = Object.assign({}, row); // copy obj
      this.dialogStatus = "review";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    handleRead(row) {
      // 打开查看弹窗
      this.temp = Object.assign({}, row); // copy obj
      this.dialogStatus = "read";
      this.dialogFormRead = true;
    },
    reviewData() {
      // 审核规则信息
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          if (this.updateDisabled) {
            this.updateDisabled = false;
            const data = {
              id: this.temp.id,
              check_status: this.temp.check_status
            };
            if (this.temp.check_remark) {
              data["check_remark"] = this.temp.check_remark;
            }
            const checkRulePath = {
              company: "STQ",
              api: "pc_order_financeCheck",
              version: "1.0",
              data: data
            };
            this.socketApi.sendSock(JSON.stringify(checkRulePath), res => {
              if (res) {
                this.socketData(res);
              } else {
                this.$message.error(" 审核失败 ");
              }
              setTimeout(() => {
                this.updateDisabled = true;
              }, 1000);
            });
          }
        }
      });
    },
    socketData(res) {
      if (res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
        } else {
          if (resj.api === "pc_order_index") {
            // 充值列表
            this.total = resj.count;
            this.list = resj.data;
          } else if (resj.api === "pc_order_financeCheck") {
            // 充值审核
            this.dialogFormVisible = false;
            this.getList();
            this.$notify({
              title: "成功",
              message: "审核成功",
              type: "success",
              duration: 2000
            });
          }
        }
      }
    },
    handleRecharge() {
      this.$router.push({
        name: "recharge"
      });
    },
    timeToData(time) {
      const date = new Date(time);
      return this.timestampToTime(date.getTime(), "{y}-{m}-{d}");
    }
  }
};
</script>
